package com.yonyou.crm.sprc.lead.ctrl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.base.doc.rmitf.IBaseDocRmService;
import com.yonyou.crm.common.enumeration.entity.WhetherEnum;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.HttpKeyConstant;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sprc.lead.entity.LeadVO;
import com.yonyou.crm.sprc.lead.rmitf.ILeadRmService;

@RestController
@RequestMapping(value = "/sprc/leads")
public class LeadCtrl {

	@Autowired
	private ILeadRmService service;
	@Autowired
	private IBaseDocRmService baseDocRmService;
	
	/**
	 * 查询线索
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException 
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object list(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);

		@SuppressWarnings("unchecked")
		Page<LeadVO> rPage = service.page(getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));
		
		return ResponseUtil.getObject(rPage);
	}
	
	/**
	 * 详情
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public Object detail(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		if (id == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "主键不能为空");
		}		
		
		return ResponseUtil.getObject(service.detail(id));
	}
	
	/**
	 * 新增线索
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		LeadVO vo = RequestUtil.getVO(request, LeadVO.class);

		String validMsg = validator(vo);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
				
		return ResponseUtil.getObject(service.insert(vo));
	}
	
	/**
	 * 修改线索
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		if (id == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "主键不能为空");
		}
		
		LeadVO vo = RequestUtil.getVO(request, LeadVO.class);

		String validMsg = validator(vo);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
		
		vo.setId(id);
				
		return ResponseUtil.getObject(service.update(vo));
	}
	
	/**
	 * 获取档案信息和档案详情
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "docs", method = RequestMethod.GET)
	public Object getDocAndDetailList(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("12","level");//等级
		map.put("13","source");//来源
		map.put("14", "post");
		Map<String, Object> baseDocMap = baseDocRmService.getDocAndDetailList(map);
		JSONObject result = new JSONObject();
		result.put("enumData", baseDocMap);
		return ResponseUtil.getObject(result);
	}
	
	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object batchDelete(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");		

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}
		
		String[] ids = idsStr.split(",");
		
		if (ids.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}

		String message = service.batchDelete(ids);
		
		Page<LeadVO> page = service.page( getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));
		
		return ResponseUtil.getObject(page, PCCodeConstant.Success, message);
	}
	
	/**
	 * 关注
	 * @param version
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value="/{id}/follow", method = RequestMethod.PUT)
	public Object follow(@PathVariable("version") String version, @PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Integer followState = (Integer)paramObject.get("followState");
		
		if (id == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "线索id不能为空");
		}
		
		if (followState.intValue() == WhetherEnum.YES.getValue()) {
			service.deleteFollow(id);
		} else {
			service.insertFollow(id);
		} 
		
		return ResponseUtil.getObject();
	}
	
	private String validator(LeadVO vo) {
		
		if (vo == null) {
			return "转换实体失败";
		}
		
		if (StringUtils.isBlank(vo.getName())) {
        	return  "姓名不能为空";
		}
		
		if (vo.getSource() == null) {
			return  "线索来源不能为空";
		}
		
		if (StringUtils.isBlank(vo.getTel())) {
			return  "固定电话不能为空";
		}
		
		return "";
	}
	
	private Page<LeadVO> getRequestPage(JSONObject paramObj) throws CrmMessageException {
		
		Integer page = paramObj.getInt(HttpKeyConstant.Page);
		Integer pageSize = paramObj.getInt(HttpKeyConstant.PageSize);
		
		if (page == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "page不能为空");
		}
		
		if (pageSize == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "pageSize不能为空");
		}
		
		return new Page<LeadVO>(page, pageSize);
	}
}
